using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;

public static class SkinnProCustomExtentions
{
    /// <summary>
    /// 递归查找物体的子物体
    /// 返回名字为name的物体，有相同名字的则返回第一个
    /// </summary>
    public static Transform Get(this Transform root, string name)
    {
        Transform result = root.Find(name);

        if (result != null)
        {
            return result;
        }

        for (int i = 0; i < root.childCount; i++)
        {
            Transform temp = root.GetChild(i).Get(name);
            if (temp != null)
            {
                return temp;
            }
        }

        return result;
    }

    public static void Reset(this Transform transform)
    {
        transform.localPosition = Vector3.zero;
        transform.localRotation = Quaternion.Euler(Vector3.zero);
        transform.localScale = Vector3.one;
    }

    /// <summary>
    /// 获取Unity Inspector上的Rotatin值
    /// </summary>
    public static Vector3 GetRotationOnInspector(this Transform transform)
    {
        System.Type transformType = transform.GetType();
        PropertyInfo m_propertyInfo_rotationOrder = transformType.GetProperty("rotationOrder", BindingFlags.Instance | BindingFlags.NonPublic);
        object m_OldRotationOrder = m_propertyInfo_rotationOrder.GetValue(transform, null);
        MethodInfo m_methodInfo_GetLocalEulerAngles = transformType.GetMethod("GetLocalEulerAngles", BindingFlags.Instance | BindingFlags.NonPublic);
        object value = m_methodInfo_GetLocalEulerAngles.Invoke(transform, new object[] { m_OldRotationOrder });
        string temp = value.ToString();
        //将字符串第一个和最后一个去掉
        temp = temp.Remove(0, 1);
        temp = temp.Remove(temp.Length - 1, 1);
        //用‘，’号分割
        string[] tempVector3;
        tempVector3 = temp.Split(',');
        //将分割好的数据传给Vector3
        Vector3 vector3 = new Vector3(float.Parse(tempVector3[0]), float.Parse(tempVector3[1]), float.Parse(tempVector3[2]));

        return vector3;
    }
}
